package it.prova.dialog;

import it.prova.Consts;
import it.prova.Utility;

import java.util.List;

import nu.xom.Document;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.resource.StringConverter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class MyDialog extends Dialog
{
//	private Button cancelButton;
	private Button okButton;
	
	private Combo combo;
	
	private Document currentModel;
	
//	private IInputValidator validator;
	
	private List<String> attributePossibleValues;
	
	private String attributeActualValue;
	private String errorMessage;
//	private String html;
//	private String message;
	private String selectedAttributeContent;
	private String selectedAttributeValue;
	private String tagName;
	private String title;
	
	private Text errorMessageText;
	private Text htmlArea;
//	private Text text;
	
	public MyDialog
	(
		Shell parentShell, 
		String title,
//		String message, 
//		String value, 
//		String html,
		String attributeActualValue,
//		List<String> attributeValues,
		Document currentModel,
		String tagName
//		IInputValidator validator
	)
	{
		super(parentShell);
		
		this.title = title;
//		this.html = html;
		this.attributeActualValue = attributeActualValue;
//		this.message = message;
//		if(value == null)
//		{
//			this.value = "";
//		}
//		else
//		{
//			this.value = value;
//		}
//		this.attributeValues = attributeValues;
		this.currentModel = currentModel;
		this.attributePossibleValues = Utility.getValues(currentModel, Consts.DATA_UNIT);
		this.tagName = tagName;
//		this.validator = validator;
	}
	
	@Override
	protected void buttonPressed(int buttonId)
	{
		if(buttonId == IDialogConstants.OK_ID)
		{
			selectedAttributeContent = htmlArea.getText();
		}
//		else
//		{
//			value = null;
//		}
		super.buttonPressed(buttonId);
	}
	
	protected void configureShell(Shell shell)
	{
		super.configureShell(shell);
		if(title != null)
		{
			shell.setText(title);
		}
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent)
	{
		// create OK and Cancel buttons by default
		okButton = createButton
		(
			parent, 
			IDialogConstants.OK_ID,
			IDialogConstants.OK_LABEL, 
			true
		);
		if(htmlArea.getText().contains(Consts.WR_PLACE_HOLDER))
		{
			okButton.setEnabled(true);
		}
		else
		{
			okButton.setEnabled(false);
		}
		
		/*Button cancelButton = */createButton
		(
			parent, 
			IDialogConstants.CANCEL_ID,
			IDialogConstants.CANCEL_LABEL, 
			false
		);
		
		//do this here because setting the text will set enablement on the ok
		// button
//		text.setFocus();
//		if(value != null)
//		{
//			text.setText(value);
//			text.selectAll();
//		}
	}

	protected Control createDialogArea(Composite parent)
	{
		// create composite
		Composite composite = (Composite) super.createDialogArea(parent);
		
		combo = new Combo(composite, SWT.DROP_DOWN);
		combo.addSelectionListener
		(
			new SelectionListener()
			{
				@Override
				public void widgetSelected(SelectionEvent event)
				{
					selectedAttributeValue = combo.getItem(combo.getSelectionIndex());
//					System.out.println("selectedValue(SelectionEvent) = " + selectedAttributeValue);
					check();
				}

				@Override
				public void widgetDefaultSelected(SelectionEvent event)
				{
					selectedAttributeValue = combo.getItem(combo.getSelectionIndex());
					check();
				}
			}
		);
		combo.addKeyListener
		(
			new KeyListener()
			{
				
				@Override
				public void keyReleased(KeyEvent arg0)
				{
					selectedAttributeValue = combo.getText();
//					System.out.println("selectedValue(ModifyEvent) = " + selectedAttributeValue);
					check();
				}
				
				@Override
				public void keyPressed(KeyEvent arg0)
				{
					// do nothing
				}
			}
		);
		combo.setItems(attributePossibleValues.toArray(new String[0]));
		combo.setLayoutData
		(
			new GridData
			(
				GridData.GRAB_HORIZONTAL | 
				GridData.HORIZONTAL_ALIGN_FILL
			)
		);
		combo.setVisibleItemCount(5);
		if(attributeActualValue != null)
		{
			int actualValueIndex = combo.indexOf(attributeActualValue);
			if(actualValueIndex >= 0)
			{
				combo.select(actualValueIndex);
				selectedAttributeValue = combo.getText();
			}
		}
		else if(combo.getItemCount() > 0)
		{
			combo.select(0);
			selectedAttributeValue = combo.getText();
		}
		
		// create message
//		if(message != null)
//		{
			Label label = new Label(composite, SWT.WRAP);
			label.setText("HTML:");
			GridData data = new GridData
			(
				GridData.GRAB_HORIZONTAL | 
				GridData.GRAB_VERTICAL | 
				GridData.HORIZONTAL_ALIGN_FILL | 
				GridData.VERTICAL_ALIGN_CENTER
			);
			data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);
			label.setLayoutData(data);
			label.setFont(parent.getFont());
//		}
//		
//		text = new Text(composite, SWT.SINGLE | SWT.BORDER);
//		text.setLayoutData(
//			new GridData
//			(
//				GridData.GRAB_HORIZONTAL| 
//				GridData.HORIZONTAL_ALIGN_FILL
//			)
//		);
//		text.addModifyListener
//		(
//			new ModifyListener()
//			{
//				public void modifyText(ModifyEvent e)
//				{
//					validateInput();
//				}
//			}
//		);
		
		htmlArea = new Text
		(
			composite, 
			SWT.MULTI | 
			SWT.BORDER | 
			SWT.H_SCROLL | 
			SWT.V_SCROLL
		);
		htmlArea.addModifyListener
		(
			new ModifyListener()
			{
				public void modifyText(ModifyEvent event)
				{
					checkHtml();
				}
			}
		);
//		GridData gridData = new GridData
//		(
//			400,
//			300
//		)
		GridData gridData = new GridData
		(
//			GridData.GRAB_HORIZONTAL | 
//			GridData.HORIZONTAL_ALIGN_FILL
			GridData.FILL_BOTH
		);
		gridData.heightHint = 300;
		gridData.widthHint = 300;
		htmlArea.setLayoutData
		(
//			new GridData
//			(
//				GridData.GRAB_HORIZONTAL | 
//				GridData.HORIZONTAL_ALIGN_FILL
//			)
			gridData
		);
		if(selectedAttributeValue != null)
		{
			String attributeContent = Utility.getAttributeContent(currentModel, selectedAttributeValue, tagName);
			
			if(attributeContent != null)
			{
				htmlArea.setText(attributeContent);
			}
		}
		
		errorMessageText = new Text(composite, SWT.READ_ONLY | SWT.WRAP);
		errorMessageText.setLayoutData
		(
			new GridData
			(
				GridData.GRAB_HORIZONTAL | 
				GridData.HORIZONTAL_ALIGN_FILL
			)
		);
		errorMessageText.setBackground
		(
			errorMessageText.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)
		);
		// Set the error message text
		// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=66292
		setErrorMessage(errorMessage);
		
		applyDialogFont(composite);
		return composite;
	}
	
	private void check()
	{
		if(attributePossibleValues.contains(selectedAttributeValue))
		{
			htmlArea.setText(Utility.getAttributeContent(currentModel, selectedAttributeValue, tagName));
			
			checkHtml();
		}
	}
	
	private void checkHtml()
	{
		if(htmlArea.getText().contains(Consts.WR_PLACE_HOLDER))
		{
			setErrorMessage(null);
			if(okButton != null)
			{
				okButton.setEnabled(true);
			}
		}
		else
		{
			setErrorMessage("Il testo inserito deve contenere il place holder '" + Consts.WR_PLACE_HOLDER + "'");
			if(okButton != null)
			{
				okButton.setEnabled(false);
			}
		}
	}
	
	protected Label getErrorMessageLabel()
	{
		return null;
	}
	
//	protected Button getOkButton()
//	{
//		return okButton;
//	}
	
//	protected Text getText()
//	{
//		return text;
//	}
//	
//	protected IInputValidator getValidator()
//	{
//		return validator;
//	}
	
	public String getSelectedAttributeContent()
	{
		return selectedAttributeContent;
	}
	
	public String getSelectedAttributeValue()
	{
		return selectedAttributeValue;
	}
	
//	public String getValue()
//	{
//		return value;
//	}
	
	public void setErrorMessage(String errorMessage)
	{
		this.errorMessage = errorMessage;
		if(errorMessageText != null && !errorMessageText.isDisposed())
		{
			errorMessageText.setText(errorMessage == null ? " \n " : errorMessage); //$NON-NLS-1$
			// Disable the error message text control if there is no error, or
			// no error text (empty or whitespace only). Hide it also to avoid
			// color change.
			// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=130281
			boolean hasError = errorMessage != null && (StringConverter.removeWhiteSpaces(errorMessage)).length() > 0;
			errorMessageText.setEnabled(hasError);
			errorMessageText.setVisible(hasError);
			errorMessageText.getParent().update();
			// Access the ok button by id, in case clients have overridden button creation.
			// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=113643
//			Control button = getButton(IDialogConstants.OK_ID);
//			if(button != null)
//			{
//				button.setEnabled(errorMessage == null);
//			}
		}
	}
	
//	protected void validateInput()
//	{
//		String errorMessage = null;
//		if(validator != null)
//		{
//			errorMessage = validator.isValid(text.getText());
//		}
//		// Bug 16256: important not to treat "" (blank error) the same as null
//		// (no error)
//		setErrorMessage(errorMessage);
//	}
}